package com.sphd.education.activitys;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Toast;

import com.sphd.education.R;
import com.sphd.education.adapter.MyViewPagerAdapter;
import com.sphd.education.base.BaseActivity;
import com.sphd.education.fragments.SearchLessonFragment;
import com.sphd.education.fragments.SearchSchoolFragment;
import com.sphd.education.info.EducationIntentKey;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 历史记录和收藏的界面可以用一个
 */
public class HistoryActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.id_tablayout)
    TabLayout idTablayout;
    @Bind(R.id.history_viewpage)
    ViewPager historyViewpage;
    private MyViewPagerAdapter mViewPagerAdapter;//viewpager的适配器
    private Intent intent;
    private Bundle bundle;
    private Context context;
    private List<Fragment> mFragments;
    private SearchLessonFragment searchLessonFragment;
    private SearchSchoolFragment searchSchoolFragment;
    private String dateflag;
    private String[] mTitles = {"浏览过的课程", "浏览过的学校"};//标题头
    private String[] mScTitles = {"收藏的课程", "收藏的学校"};//标题头

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        ButterKnife.bind(this);


        initView();

    }

    @Override
    public void initView() {
        intent = getIntent();
        context = HistoryActivity.this;
        mFragments = new ArrayList<Fragment>();

        bundle = intent.getBundleExtra("mine");
        dateflag=bundle.getString(EducationIntentKey.FLAGACTIVITY);
//        Toast.makeText(context, "" + bundle.getString(EducationIntentKey.FLAGACTIVITY), Toast.LENGTH_SHORT).show();
        if(dateflag.equals(EducationIntentKey.HISTORY)){
            toolbar.setTitle("历史浏览");
        }else if(dateflag.equals(EducationIntentKey.COLLECT)){
            toolbar.setTitle("我的收藏");
        }
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();//这是自带的箭头的返回的监听
            }
        });
        bundle.putString(EducationIntentKey.FLAG, dateflag);
        searchLessonFragment = SearchLessonFragment.newInstance(bundle);
        searchSchoolFragment = SearchSchoolFragment.newInstance(bundle);
        mFragments.add(searchLessonFragment);
        mFragments.add(searchSchoolFragment);   // Tab的标题采用string-array的方法保存，在res/values/arrays.xml中写
        if(dateflag.equals(EducationIntentKey.HISTORY)){
            mViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), mTitles, mFragments);
        }else if(dateflag.equals(EducationIntentKey.COLLECT)){
            mViewPagerAdapter = new MyViewPagerAdapter(getSupportFragmentManager(), mScTitles, mFragments);
        }

        historyViewpage.setAdapter(mViewPagerAdapter);
        // 设置ViewPager最大缓存的页面个数
//        idViewpager.setOffscreenPageLimit(7);
        // 给ViewPager添加页面动态监听器（为了让Toolbar中的Title可以变化相应的Tab的标题）
        idTablayout.setTabMode(TabLayout.MODE_FIXED);//MODE_FIXED按权重显示MODE_SCROLLABLE按具体大小显示
        // 将TabLayout和ViewPager进行关联，让两者联动起来
        idTablayout.setupWithViewPager(historyViewpage);
        idTablayout.setTabTextColors(getResources().getColor(R.color.gray), getResources().getColor(R.color.white));
        // 设置Tablayout的Tab显示ViewPager的适配器中的getPageTitle函数获取到的标题
        idTablayout.setTabsFromPagerAdapter(mViewPagerAdapter);
        historyViewpage.addOnPageChangeListener(this);
        historyViewpage.setCurrentItem(0);

    }

    @Override
    public void findViewById() {

    }

    @Override
    public void myNet() {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {

    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
